package ma.cdm.compteRendu.servlets;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ma.cdm.Utils.MimeType;
import ma.cdm.rapports.ReportProcessing;
import ma.cdm.securite.CDMKeyGen;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller

@RequestMapping("/downloadReport.form")
public class DownloadReport extends HttpServlet {
	
	private static final long serialVersionUID = 8617174166115669467L;

	@Resource
	private ReportProcessing reportProcessing;

	@RequestMapping(method=RequestMethod.GET)	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException {
		OutputStream out=null;	        
	try{
			HttpSession session = request.getSession();
			if(session == null || session.getAttribute("login") == null || session.getAttribute("filePath") == null)
			{
				return;
			}
			
	        String decryptedPath = CDMKeyGen.getInstance(session.getId()).decrypt((String)session.getAttribute("filePath"));
        
			File file = new File(decryptedPath);
            if(!file.exists())
            {
            	return;
            }
            String extention = ".pdf";
            String  mimeType =MimeType.pdf;
            if(file.getName().toLowerCase().endsWith("swf"))
            {
            	mimeType = MimeType.swf;
            	extention = ".swf";
            }
            else if(file.getName().toLowerCase().endsWith("pdf"))
            {
            	mimeType = MimeType.pdf;
            	extention = ".pdf";
            }
            else if(file.getName().toLowerCase().endsWith("docx"))
            {
            	mimeType = MimeType.docx;
            	extention = ".docx";
            }
            
            
            
		    byte[] data=FileUtils.readFileToByteArray(file);
			response.setContentType(mimeType);
			response.setHeader("Expires", "0");
			response.setHeader("Cache-Control",	"must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Content-Disposition","attachment; filename=rapport"+extention);
			response.setHeader("Pragma", "public");
			
			out =response.getOutputStream();
			out.write(data);
			response.setContentLength(data.length);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(out !=null){
				try {
					out.close();
				} catch (IOException e) {
				}
			}
	}	
  }

	public ReportProcessing getReportProcessing() {
		return reportProcessing;
	}

	public void setReportProcessing(ReportProcessing reportProcessing) {
		this.reportProcessing = reportProcessing;
	}

	
	
}
